package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;

import java.util.ArrayList;
import java.util.List;

public class MorePlanMessageActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_plan_message);
        initData();
        initTitle();
        initView();
    }

    private int position;
    private String stu;

    private void initData() {
        messages = (List<String>) getIntent().getSerializableExtra("moreData");
        position = getIntent().getIntExtra("position", 0);
        stu = getIntent().getStringExtra("status");
        if ("2".equals(stu) || "3".equals(stu) || "4".equals(stu)) {
            findViewById(R.id.add_product).setVisibility(View.GONE);
        }
    }

    private List<String> messages = new ArrayList<>();
    private SwipeMenuListView product;
    private MyAdapter mAdapter;

    private void initView() {
        product = (SwipeMenuListView) findViewById(R.id.product);
        product.setFlag(true);
        mAdapter = new MyAdapter(messages);
        product.setAdapter(mAdapter);
        SwipeMenuCreator creater = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {


                //同理create删除item
                SwipeMenuItem deleteItem = new SwipeMenuItem(MorePlanMessageActivity.this);
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set item title
                deleteItem.setTitle("删除");
                // set item title fontsize
                deleteItem.setTitleSize(12);
                // set item title font color
                deleteItem.setTitleColor(Color.WHITE);
                // set a icon
                //   deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        if (!("2".equals(stu) || "3".equals(stu) || "4".equals(stu))) {
            product.setMenuCreator(creater);
        }
        product.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(final int position, SwipeMenu menu, int index) {
                switch (index) {

                    case 0:
                        //删除的逻辑
                        showDialogv7("确定删除吗？", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                messages.remove(position);
                                mAdapter.notifyDataSetChanged();
                            }
                        });
                        break;

                    default:
                        break;
                }
                return false;
            }
        });
        product.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!("2".equals(stu) || "3".equals(stu) || "4".equals(stu))) {
                    addMessage(true, position);
                }
            }
        });
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("更多内容");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);

        submit.setText("保存");
        submit.setVisibility(View.VISIBLE);
        if ("2".equals(stu) || "3".equals(stu) || "4".equals(stu)) {
            submit.setVisibility(View.GONE);
        }
        findViewById(R.id.add_product).setOnClickListener(this);
        submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                Intent intent = new Intent();
                ArrayList<String> message = (ArrayList<String>) messages;
                intent.putExtra("moreData", message);
                intent.putExtra("position", position);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.add_product:
                addMessage(false, 0);
                break;
        }
    }

    private void addMessage(final boolean flag, final int position) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.dialog_more_message, null);
        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        TextView queding = (TextView) view.findViewById(R.id.queding2);
        final EditText edit = (EditText) view.findViewById(R.id.edit);
        if (flag) {
            edit.setText(messages.get(position));
        }
        builder.setView(view);
        final AlertDialog show = builder.show();
        Window dialogWindow = show.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);
        // lp.x = 100; // 新位置X坐标
        lp.width = getWindowManager().getDefaultDisplay().getWidth() / 3 * 2; // 宽度
        dialogWindow.setAttributes(lp);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (edit.getText().toString().equals("")) {
                    MyToast("请填写详细内容");
                    return;
                }
                if (flag) {
                    messages.set(position, edit.getText().toString());
                } else {
                    messages.add(edit.getText().toString());
                }
                mAdapter.notifyDataSetChanged();
                show.dismiss();
            }
        });
    }

    class MyAdapter extends BaseAdapter {
        private List<String> str = new ArrayList<>();

        public MyAdapter(List<String> str) {
            this.str = str;
        }

        @Override
        public int getCount() {
            return str.size();
        }

        @Override
        public Object getItem(int position) {
            return str.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = View.inflate(MorePlanMessageActivity.this, R.layout.item_more_plan, null);
                holder.detail_message = (TextView) convertView.findViewById(R.id.detail_message);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.detail_message.setText(str.get(position));
            return convertView;
        }

        class Holder {
            TextView detail_message;
        }
    }
}
